package com.hnxxxy.sysmgr.controller;

import com.github.pagehelper.PageHelper;
import com.hnxxxy.common.AjaxRtnEntity.AjaxRtnEntity;
import com.hnxxxy.common.annotation.LogAnno;
import com.hnxxxy.common.tools.Help;
import com.hnxxxy.common.tools.TableObj;
import com.hnxxxy.sysmgr.common.Constant;
import com.hnxxxy.sysmgr.pojo.Staff;
import com.hnxxxy.sysmgr.service.StaffService;
import com.hnxxxy.sysmgr.vo.StaffVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author Json
 * @date 2020-26-8:16
 */
@RestController
@RequestMapping("staff")
public class StaffController {

    @Autowired
    private StaffService staffService;

    /**
     * 查询所有员工
     * @param obj
     * @param staffVo
     * @return
     */
    @RequestMapping("loadAllStaff")
    @LogAnno(description = "查询所有员工")
    public TableObj queryAllStaffForList(TableObj obj, StaffVo staffVo){
        PageHelper.startPage(obj.getCurr(), obj.getNums());
        List<Staff> list = staffService.queryAllStaff(staffVo);
        return Help.getPageTableRtn(obj,list);
    }


    /**
     * 加载员工管理的分配角色的数据
     * @param staffVo
     * @return
     */
    @RequestMapping("initRoleByStaffId")
    @LogAnno(description = "加载员功管理的分配角色的数据")
    public TableObj initRoleByStaffId(StaffVo staffVo){

        return staffService.queryStaffRole(staffVo.getId());
    }

    /**
     * 保存用户和角色的关系
     */
    @RequestMapping("saveStaffRole")
    @LogAnno(description = "加载员功管理的分配角色的数据",descriptiontype = "saveStaffRole")
    public AjaxRtnEntity saveStaffRole(StaffVo staffVo) {
        return new AjaxRtnEntity(this.staffService.saveStaffRole(staffVo),"分配成功！",null);
}

    /**
     * 添加员工
     */
    @RequestMapping("addStaff")
    @LogAnno(description = "添加员工",descriptiontype = "addStaff")
    public AjaxRtnEntity addStaff(StaffVo staffVo) {
        staffVo.setCreatedate(new Date());
        String salt= UUID.randomUUID().toString();
        staffVo.setSalt(salt);
        staffVo.setPwd(new Md5Hash(Constant.USER_DEFAULT_PWD,salt,1000).toBase64());
       return new AjaxRtnEntity(this.staffService.addStaff(staffVo),"添加成功！",null);
    }
    /**
     * 修改员工
     */
    @RequestMapping("updateStaff")
    @LogAnno(description = "更新员工",descriptiontype = "updateStaff")
    public AjaxRtnEntity updateStaff(StaffVo staffVo) {
        return new AjaxRtnEntity(this.staffService.updateStaff(staffVo),"更新成功！",null);
    }

    /**
     * 删除员工
     */
    @RequestMapping("deleteStaff")
    @LogAnno(description = "删除员工",descriptiontype = "deleteStaff")
    public AjaxRtnEntity deleteStaff(Integer id) {
        return new AjaxRtnEntity(this.staffService.deleteStaff(id),"删除成功！",null);
    }
    /**
     * 批量删除员工
     */
    @RequestMapping("batchDeletestaff")
    @LogAnno(description = "批量删除员工",descriptiontype = "batchDeletestaff")
    public AjaxRtnEntity deleteBatchStaffs(StaffVo staffVo) {
       return new AjaxRtnEntity(this.staffService.batchDeletestaff(staffVo.getIds()),"删除成功！",null);
    }

    /**
     * 重置密码
     * @param id
     */
    @RequestMapping("resetPwd")
    @LogAnno(description = "重置密码",descriptiontype = "resetPwd")
    public AjaxRtnEntity resetPwd(Integer id){
        StaffVo staff = new StaffVo();
        staff.setId(id);
        String salt= UUID.randomUUID().toString();
        staff.setSalt(salt);
        staff.setPwd(new Md5Hash(Constant.USER_DEFAULT_PWD,ByteSource.Util.bytes(salt),1000).toBase64());
        return new AjaxRtnEntity(this.staffService.updateStaffByStaffId(staff),"重置成功！",null);

    }

    /**
     * 更改密码
     * @param id 当前用户的id
     * @param pwd 原来的密码
     * @param newPwd 输入的新密码
     * @return
     */
    @RequestMapping("updatePwd")
    @LogAnno(description = "更改密码", descriptiontype = "updatePwd")
    public AjaxRtnEntity updatePwd(Integer id, String pwd, String newPwd){

        StaffVo staffVo = new StaffVo();

        Subject subject = SecurityUtils.getSubject();
        Staff staff = (Staff)subject.getPrincipal();

        String staffpwd = staff.getPwd();
        String saltPwd = new Md5Hash(pwd, ByteSource.Util.bytes(staff.getSalt()), 1000).toBase64();

        if (!staffpwd.equals(saltPwd)){

            return new AjaxRtnEntity(0,"输入的旧密码不正确！",null);
        }else {

            staffVo.setId(id);
            String salt= UUID.randomUUID().toString();
            staffVo.setSalt(salt);
            staffVo.setPwd(new Md5Hash(newPwd,ByteSource.Util.bytes(salt),1000).toBase64());
            return new AjaxRtnEntity(this.staffService.updatePwdByStaffId(staffVo),"修改成功，请重新登录！",null);
        }
    }
}
